package com.ruoyi.energize.controller;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.energize.domain.WdzDepartmentsVo;
import com.ruoyi.energize.service.IWdzDepartmentsService;
import com.ruoyi.energize.util.GetGenderData;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.energize.domain.WdzPassengerFlowData;
import com.ruoyi.energize.service.IWdzPassengerFlowDataService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 门店客流数据Controller
 *
 * @author ruoyi
 * @date 2023-06-14
 */
@RestController
@RequestMapping("/energize/passengerFlowData")
@Component //使spring管理
public class WdzPassengerFlowDataController extends BaseController
{
    @Autowired
    private IWdzPassengerFlowDataService wdzPassengerFlowDataService;

    @Autowired
    private IWdzDepartmentsService wdzDepartmentsService;

    /**
     * 通过门店列表，生成对应的客流数据
     * 生成昨日报表信息接口
     */
   // @GetMapping("/getSyncYesterdayShopsPassengerIndicatorData")
    @Scheduled(cron = "0 0 1 * * ?")
    public void getSyncYesterdayShopsPassengerIndicatorData(){
        System.out.println("每日上午1点执行一次同步客流信息脚本");
        String timeType="2";
        LocalDate localDate = LocalDate.now(); //获取今天的日期
        LocalDate yesterday = localDate.plusDays(-1); //前一天日期是今天减1
        String startTime=DateTimeFormatter.ofPattern("yyyy-MM-dd 00:00:00").format(yesterday);
        String endTime=DateTimeFormatter.ofPattern("yyyy-MM-dd 23:59:59").format(yesterday);
        //查询出所有的万店掌门店id字符串
        String ids=wdzDepartmentsService.selectWdzDepartmentsIds();
        //1、按照天同步客流量
        GetGenderData getGenderData=new GetGenderData();
        String shopsPassengerIndicatorDataString=getGenderData.getManyShopsPassengerIndicatorData(ids,startTime,endTime,timeType);
        this.stringToJsonobject(shopsPassengerIndicatorDataString,timeType);
        //2、再按照小时统计客流量
        timeType="1";
        String shopsPassengerIndicatorDataString2=getGenderData.getManyShopsPassengerIndicatorData(ids,startTime,endTime,timeType);
        this.stringToJsonobject(shopsPassengerIndicatorDataString2,timeType);

    }

    /**
     * 通过门店列表，生成对应的客流数据
     * 生成2023-04-10至2023-06-15日报表信息接口
     */
   // @GetMapping("/getManyShopsPassengerIndicatorData")
    public void getManyShopsPassengerIndicatorData(){
        String timeType="2";
       /* LocalDate startDate = LocalDate.of(2023, 04, 10);
        LocalDate endDate = LocalDate.of(2023, 06, 15);*/
        LocalDate startDate = LocalDate.of(2023, 06, 21);
        LocalDate endDate = LocalDate.of(2023, 07, 03);
        String startTime=DateTimeFormatter.ofPattern("yyyy-MM-dd 00:00:00").format(startDate);
        String endTime=DateTimeFormatter.ofPattern("yyyy-MM-dd 23:59:59").format(endDate);
        //查询出所有的万店掌门店id字符串
        String ids=wdzDepartmentsService.selectWdzDepartmentsIds();
        //1、按照天同步客流量
        GetGenderData getGenderData=new GetGenderData();
        String shopsPassengerIndicatorDataString=getGenderData.getManyShopsPassengerIndicatorData(ids,startTime,endTime,timeType);
        this.stringToJsonobject(shopsPassengerIndicatorDataString,timeType);
        //2、再按照小时统计客流量
        timeType="1";
        String shopsPassengerIndicatorDataString2=getGenderData.getManyShopsPassengerIndicatorData(ids,startTime,endTime,timeType);
        this.stringToJsonobject(shopsPassengerIndicatorDataString2,timeType);
    }

    public void stringToJsonobject(String stringJson,String timeType){
        JSONObject jsonObject = (JSONObject) JSONObject.parse(stringJson);
        //获取 msg
        if(jsonObject.get("result").equals("请求数据成功!")){
            JSONArray shopDataList= jsonObject.getJSONArray("data");

            List<WdzDepartmentsVo> wdzDepartmentsVoList=new ArrayList<>();
            wdzDepartmentsVoList=shopDataList.toJavaList(WdzDepartmentsVo.class);
            List<WdzPassengerFlowData> wdzPassengerFlowDataList=new ArrayList<>();
            if(wdzDepartmentsVoList.size()>0){
                for (WdzDepartmentsVo wdzDepartmentsVo:wdzDepartmentsVoList){
                    //根据部门id查询烟草门店id

                    //将客流分条数据赋值插入到数据库中
                   wdzPassengerFlowDataList=wdzDepartmentsVo.getDataList();
                    if (wdzPassengerFlowDataList.size()>0) {//说明查询到相关数据
                        //使用list批量插入到万店掌门店列表中去
                        for (WdzPassengerFlowData wdzPassengerFlowData:wdzPassengerFlowDataList){
                            wdzPassengerFlowData.setDepId(wdzDepartmentsVo.getDepId());
                            wdzPassengerFlowData.setShopId(wdzDepartmentsVo.getShopId());
                            wdzPassengerFlowData.setName(wdzDepartmentsVo.getName());
                            wdzPassengerFlowData.setTimeType(Integer.valueOf(timeType));
                            wdzPassengerFlowDataService.insertWdzPassengerFlowData(wdzPassengerFlowData);
                        }
                    }
                }
            }

        }
    }

    /**
     * 查询门店客流数据列表
     */
    @PreAuthorize("@ss.hasPermi('energize:passengerFlowData:list')")
    @GetMapping("/list")
    public TableDataInfo list(WdzPassengerFlowData wdzPassengerFlowData)
    {
        startPage();
        List<WdzPassengerFlowData> list = wdzPassengerFlowDataService.selectWdzPassengerFlowDataList(wdzPassengerFlowData);
        return getDataTable(list);
    }

    /**
     * 导出门店客流数据列表
     */
    @PreAuthorize("@ss.hasPermi('energize:passengerFlowData:export')")
    @Log(title = "门店客流数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WdzPassengerFlowData wdzPassengerFlowData)
    {
        List<WdzPassengerFlowData> list = wdzPassengerFlowDataService.selectWdzPassengerFlowDataList(wdzPassengerFlowData);
        ExcelUtil<WdzPassengerFlowData> util = new ExcelUtil<WdzPassengerFlowData>(WdzPassengerFlowData.class);
        util.exportExcel(response, list, "门店客流数据数据");
    }

    /**
     * 获取门店客流数据详细信息
     */
    @PreAuthorize("@ss.hasPermi('energize:passengerFlowData:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(wdzPassengerFlowDataService.selectWdzPassengerFlowDataById(id));
    }

    /**
     * 新增门店客流数据
     */
    @PreAuthorize("@ss.hasPermi('energize:passengerFlowData:add')")
    @Log(title = "门店客流数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WdzPassengerFlowData wdzPassengerFlowData)
    {
        return toAjax(wdzPassengerFlowDataService.insertWdzPassengerFlowData(wdzPassengerFlowData));
    }

    /**
     * 修改门店客流数据
     */
    @PreAuthorize("@ss.hasPermi('energize:passengerFlowData:edit')")
    @Log(title = "门店客流数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WdzPassengerFlowData wdzPassengerFlowData)
    {
        return toAjax(wdzPassengerFlowDataService.updateWdzPassengerFlowData(wdzPassengerFlowData));
    }

    /**
     * 删除门店客流数据
     */
    @PreAuthorize("@ss.hasPermi('energize:passengerFlowData:remove')")
    @Log(title = "门店客流数据", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(wdzPassengerFlowDataService.deleteWdzPassengerFlowDataByIds(ids));
    }
}
